<?php

namespace App\Http\Controllers;

use App\Models\Album;
use App\Models\Comment;
use App\Models\Complaint;
use App\Models\Complainttype;
use App\Models\Love;
use App\Models\Travelnote;
use App\Models\Wxuser;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;


class UserController extends WxBaseController
{
    //登录
    public function login()
    {
        $input = request();
        $result = $this->get_openid($input->code);
        $openid = $result['openid'];
        if($openid){
            $uinfo = $input->userInfo;
            $token = $this->generateToken($openid);
            if ($this->firstLogin($openid)) {
                $info = [
                    'openid' => $openid,
                    'token' => $token,
                    'nickname' => base64_encode($uinfo['nickName']),
                    'avatarUrl' => $uinfo['avatarUrl'],
                    'gender' => $uinfo['gender'],
                    'city' => $uinfo['city'],
                    'province' => $uinfo['province'],
                    'country' => $uinfo['country']
                ];
                if($data=$this->dologin($info)){
                    return $this->apiReturn(true,'登录成功',['token'=>$data->token,'score'=>0]);
                }else{
                    return $this->apiReturn(false,'登录失败');
                }
            } else {
                $newToken = $this->refreshToken($openid);
                $user = $this->auth($openid);
                return $this->apiReturn(true, '登录成功',['token'=>$newToken,'score'=>$user->score]);
            }
        }else{
            return $this->apiReturn(false,'无法获取openid');
        }
    }
    public function checkToken(){
        if($this->hasToken()){
            if($auth = $this->auth()){
                if(Cache::has($auth->openid))
                    return $this->apiReturn(true,'success');
            }
        }
        return $this->apiReturn(false,'401');
    }
    //我的相册
    public function album(){
        $arr=[];
        $albums=Album::where('wxuser_id',$this->auth()->id)->select('coverimage')->get();
        foreach ($albums as $val){
            array_push($arr,$val->coverimage);
        }
        $notes=Travelnote::where('wxuser_id',$this->auth()->id)->select('coverimage')->get();
        foreach ($notes as $val){
            array_push($arr,$val->coverimage);
        }
        return $this->apiReturn(true,'success',$arr);
    }
    //我的收藏
    public function love(){
        $loves=Love::where('wxuser_id',$this->auth()->id)->select('id','target_id','model')->get();
        foreach ($loves as $item) {
            $model = DB::table($item->model);
            $info=$model->find($item->target_id);
            $item->title=$info->title;
            $item->coverimage=config('domain').'/'.$info->coverimage;
            if($item->model=='albums'&&$info->recommend==0){
                $item->coverimage=$info->coverimage;
            }
            if($item->model=='travelnotes'&&$info->is_admin==1){
                $item->coverimage=$info->coverimage;
            }
        }
        return $this->apiReturn(true,'success',$loves);
    }
    //我的游记
    public function note(){
        $notes=Travelnote::where('wxuser_id',$this->auth()->id)->get();
        return $this->apiReturn(true,'success',$notes);
    }
    //我的评论
    public function comment(){
        $comments=Comment::where('wxuser_id',$this->auth()->id)->get();
        return $this->apiReturn(true,'success',$comments);
    }
    //我的投诉
    public function complaint(){
        $complaints=Complaint::where('wxuser_id',$this->auth()->id)->get();
        foreach ($complaints as $val) {
            $val->type = Complainttype::find($val->type)->title;
        }
        return $this->apiReturn(true,'success',$complaints);
    }
}